//
//  DM_SQLBuilder.swift
//  构建者模式_ORM_SWIFT
//
//  Created by macOS on 2017/4/22.
//  Copyright © 2017年 macOS. All rights reserved.
//

import Foundation



//一般写法
//查询数据库
//客户端直接定义SQL语句
//let sql = "select * from t_user where t_id = 10"
//let result = db.execute(sql)

//SQL构建过程应该和表示进行分离(也就是客户端不需要直接定义SQL语句,而是通过构建者帮助完成)

//分析:
//SQL 语句分为(查询 插入 删除 更新 创建表 删除表 等等)
//针对语法差别,如何设计?
//解决方案: 分析类结构
class DM_SQLBuilder: NSObject {
    
    var sql = ""
    
    
    func talbe(_ table:String) -> DM_SQLBuilder {
        sql.append(table)
        sql.append(" ")
        return self
    }
    
    func wh() -> DM_SQLBuilder {
        sql.append("where ")
        return self
    }
    
    
    func whAnd(_ wh: [String : Any]) -> DM_SQLBuilder {
        
        return wsj_where(isAnd: true, wh: wh)
        
    }
    
    //防止sql 注入的 and 写法
    func whAndFormat(_ wh: [String : Any]) -> DM_SQLBuilder {
        return wsj_where(isFormat: true, isAnd: true, wh: wh)
    }
    
    func whOr(_ wh: [String:Any]) -> DM_SQLBuilder {
        
        return wsj_where(isOr: true, wh: wh)
    }
    
    
    //默认不格式化(where 后面不加?)
    func wsj_where(isFormat: Bool = false, isAnd: Bool = false, isOr: Bool = false, wh: [String : Any]) -> DM_SQLBuilder {
        var index = 0
        
        for key in wh.keys {
            sql.append(key)
            sql.append(" = ")
            if isFormat {
                sql.append("? ")
            }else{
           
                if let tem_key = wh[key] {
                    sql.append(String(describing: tem_key))
                }
               
            }
           
            
            //需要做一个处理 : 什么时候拼接and 什么时候不拼接
            
            index+=1
            if index < wh.count {
                if isAnd {
                    sql.append(" and ")
                }else if isOr {
                    sql.append(" or ")
                }
            }
            
        }
        
        return self
    }
    
    //注入 字段
    func colums(_ colums: Array<String>) -> DM_SQLBuilder {
      
        var index = 0
        
        for colum in colums {
            index+=1
            sql.append(colum)
            if index < colums.count {
                sql.append(",")
            }
            
        }
        
        sql.append(" ")
        return self
    }
    
    //注入 值
    func wsj_values(_ values: Array<Any>) -> DM_SQLBuilder {
        
        var index = 0
        
        for value in values {
            index+=1
            appendValue(value)
            if index < values.count {
                sql.append(",")
            }
            
        }
        
        sql.append(" ")
        return self
    }

    
    //专门处理类型
    func appendValue(_ value: Any) {
        
        switch value {
        case let someInt as Int:
        sql.append("\(someInt)")
           break
        case let someFloat as Float:
        sql.append("\(someFloat)")
           break
        case let someDouble as Double:
        sql.append("\(someDouble)")
           break
        case let someString as String:
        
        sql.append("\'\(someString)\'")
          break
          
        default:
          break
        }
    }
   
    func builder() -> String {
        return sql.appending(";")
    }
    

}
